home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK4CM1.TXT < prev    next >
Text File  |  1996-02-28  |  15KB  |  404 lines

  1. Week 4 course material
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. A Crash Course in Programming the Windows Graphical User Interface (GUI)
  7. ==============================================================================
  8.  
  9. What is a GUI (graphical user interface) anyway?  This probably means
  10. different things to different people but to most it means a way of
  11. controlling a computer that involves manipulating windows (rectangular areas
  12. displayed on a computer screen) using a mouse or other pointing device.
  13.  
  14. If you are using Microsoft WINDOWS then you are using a software system with
  15. a GUI (we will use all uppercase when referring to Microsoft's product and
  16. not to any kind of window in general).  WINDOWS isn't just a GUI, but it does
  17. include one.
  18.  
  19. You have been using the WINDOWS GUI all along while working through this
  20. Liberty BASIC course.  The programs that you've written each run in a window
  21. that is managed by the WINDOWS GUI.  This simple text-only window is called
  22. a 'main window' in Liberty BASIC lingo.  This part of the course is
  23. concerned with teaching how to do fancier things in the way of designing
  24. windows for your programs.
  25.  
  26. In this section we will learn:
  27.  
  28. 1) How to use Liberty BASIC to open windows in the WINDOWS GUI;
  29. 2) How to set the size, position, and type of our windows;
  30. 3) How to add buttons and other controls to our windows;
  31. 4) How to program the buttons and other controls to do what we want
  32.  
  33. Note: All of the program examples in this section are also provided as
  34. individual program files.
  35.  
  36.  
  37. Your First Window
  38. ==============================================================================
  39.  
  40. Type in the following short program and run it.  Insert your name in the
  41. place where it says myname.
  42.  
  43.     'WK4PROG1.BAS
  44.     'open a window
  45.     open "myname's first window!" for window as #myFirst
  46.  
  47.     'now stop and wait
  48.     input aVar$
  49.  
  50.     'now close the window
  51.     close #myFirst
  52.  
  53.     end
  54.  
  55. A window will appear with your name in the title!  That was easy, wasn't it?
  56. Now click on the program's initial window (called its 'main window') and
  57. press Enter to respond to the INPUT statement.  The window will close and the
  58. program will end.
  59.  
  60. Liberty BASIC uses file statements like OPEN, CLOSE, INPUT, and PRINT to
  61. manage and control windows.  Once a window is opened, we can send it
  62. commands by PRINTing to it, and we can get information by INPUTting from
  63. it.
  64.  
  65. Note: There is nothing to prevent a Liberty BASIC program from opening
  66. more than one window as demonstrated.  All you need is multiple OPEN
  67. statements, and each window must have a unique handle.
  68.  
  69.  
  70. Adjusting Window Size and Position
  71. ------------------------------------------------------------------------------
  72.  
  73. Let's change the size of our Window.  This is done by changing the value of
  74. two special variables WindowWidth and WindowHeight before opening the window.
  75. We can also determine the opening position of the window by adjusting two
  76. other special variables UpperLeftX and UpperLeftY.  This positioning is
  77. relative to the upper-left corner of the display screen.  Take a look at this
  78. example:
  79.  
  80.     'WK4PROG2.BAS
  81.     'open a window sized at 300 by 100 at position 200, 150
  82.     WindowWidth = 300
  83.     WindowHeight = 100
  84.     UpperLeftX = 200
  85.     UpperLeftY = 150
  86.     open "myname's first window!" for window as #myFirst
  87.  
  88.     'now stop and wait
  89.     input aVar$
  90.  
  91.     'now close the window
  92.     close #myFirst
  93.  
  94.     end
  95.  
  96.  
  97. Adding a button
  98. ------------------------------------------------------------------------------
  99.  
  100. Now let's add a button to our window.  Look at the following short program:
  101.  
  102.     'WK4PROG3.BAS
  103.     'open a window with a button
  104.     'the window is sized at 399 by 100 at position 200, 150
  105.     WindowWidth = 300
  106.     WindowHeight = 100
  107.     UpperLeftX = 200
  108.     UpperLeftY = 150
  109.     button #myFirst.ok, "OK!", [okClicked], UL, 15, 15
  110.     open "myname's first window!" for window as #myFirst
  111.  
  112.     'now stop and wait
  113.     input aVar$
  114.     goto [quit]
  115.  
  116. [okClicked]  'the OK! button was clicked
  117.  
  118.     notice "You clicked on OK!"
  119.  
  120. [quit]
  121.     'now close the window
  122.     close #myFirst
  123.  
  124.     end
  125.  
  126. Notice the BUTTON statement we placed before the OPEN statement.  This must
  127. be inserted BEFORE the OPEN statement.  Let's break that line of code down
  128. to explain how it works.
  129.  
  130.     button #myFirst.ok, "OK!", [okClicked], UL, 15, 15
  131.  
  132. The first item in the BUTTON statement is a handle for the button.  In this
  133. case our handle is #myFirst.ok.  We are adding this button to a window that
  134. will have a handle of #myFirst, and we add .ok to extend the handle so that
  135. the button will have its own handle different from the handle of the window.
  136.  
  137. The second item is the [okClicked] branch label.  This tells Liberty BASIC
  138. to perform an implicit GOTO [okClicked] when the button is clicked on.  In
  139. this case the NOTICE statement causes a dialog box to appear that displays
  140. "You clicked on OK!".  Once you clear the dialog box the window will be
  141. closed and the program will end.
  142.  
  143. The third item (UL) tells Liberty BASIC that the button will be positioned
  144. relative to the upper-left corner of the window.  There are also UR, LL, and
  145. LR (for placing buttons relative to the upper-right, lower-left and lower-
  146. right corners).
  147.  
  148. The fourth and fifth items specify how many pixels from the (in this case
  149. upper-left) corner to place the button.
  150.  
  151. One option is to specify the exact width and height of the button.  Liberty
  152. BASIC automatically chooses the size of the button if you don't specify it.
  153. Here's the same button, but we'll change the size to 75 pixels wide and 50
  154. pixels high:
  155.  
  156.     button #myFirst.ok, "OK!", [okClicked], UL, 15, 15, 75, 50
  157.  
  158. The INPUT statement in our program is very important.  Liberty BASIC will
  159. only respond to actions performed on buttons or other controls when a
  160. running program is waiting at an INPUT statement (or a SCAN statement, but
  161. we won't cover that here).
  162.  
  163.  
  164. Adding an Entry Field with TEXTBOX
  165. ==============================================================================
  166.  
  167. Now let's add a textbox control to our window that let's us display and edit
  168. a line of text.  We will use the TEXTBOX statement to accomplish this.  Look
  169. at this code:
  170.  
  171.     'WK4PROG4.BAS
  172.     'open a window with a button and a textbox
  173.     'the window is sized at 300 by 100 at position 200, 150
  174.     WindowWidth = 300
  175.     WindowHeight = 100
  176.     UpperLeftX = 200
  177.     UpperLeftY = 150
  178.     button #myFirst.ok, "OK!", [okClicked], UL, 15, 15
  179.     textbox #myFirst.field, 60, 15, 200, 25
  180.     open "myname's first window!" for window as #myFirst
  181.  
  182.     'now print some text into the textbox
  183.     print #myFirst.field, "Type some text here."
  184.  
  185.     'now stop and wait
  186.     input aVar$
  187.     goto [quit]
  188.  
  189. [okClicked]  'OK! was clicked.  Get the contents of the entry field
  190.  
  191.     'print a command to the textbox
  192.     print #myFirst.field, "!contents?"
  193.     'now get the contents from the textbox
  194.     input #myFirst.field, aString$
  195.  
  196.     'now pop up a notice saying what was in the textbox
  197.     notice aString$
  198.  
  199. [quit]
  200.     'now close the window
  201.     close #myFirst
  202.  
  203.     end
  204.  
  205.  
  206. Run the example and see how it works!  Notice the TEXTBOX statement after
  207. the BUTTON statement.  Its format is very simple.
  208.  
  209.     textbox #myFirst.field, 60, 15, 200, 25
  210.  
  211. The first item #myFirst.field is a handle for the textbox.  This is just like
  212. the handle we gave to our button.
  213.  
  214. The next two items 60 and 15 are the placement of the control relative to
  215. the upper-left corner of the window.
  216.  
  217. The last two items 200 and 25 are the width and height of the control.  If
  218. it isn't wide enough, the end of the line will not appear.  If it isn't tall
  219. enough, you won't see any text in the control at all.
  220.  
  221. After our OPEN statement, see how we print some text "Type some text here."
  222. into our textbox control.  This is the text that you see in the control when
  223. the program is run.
  224.  
  225. Now look at how we changed the code after our [okClicked] branch label.
  226. Instead of just displaying the same message every time it displays the text
  227. in our textbox control.  We accomplish this by printing a command to the
  228. textbox with the line:
  229.  
  230.     print #myFirst.field, "!contents?"
  231.  
  232. This tells Liberty BASIC that we want the contents of that textbox control.
  233. Then using the following line we get that string and insert it into the
  234. variable aString$:
  235.  
  236.     input #myFirst.field, aString$
  237.  
  238. When printing a command to a textbox control, the command must start with an
  239. exclamation point.  This is how Liberty BASIC knows that we want to send a
  240. command to the textbox.  The contents of our textbox would have been replaced
  241. with "contents?" if we had left out the exclamation point like so:
  242.  
  243.     print #myFirst.field, "contents?"
  244.  
  245.  
  246. Labeling with Statictext Controls
  247. ==============================================================================
  248.  
  249. So far our window designs have been simple enough that we haven't needed to
  250. mark any part of our window with a descriptive label or annotation.  This
  251. is an important matter in many applications where more than one textbox (or
  252. other kind of control) is used.  Here is a version of our program that uses
  253. a statictext control as a label:
  254.  
  255.  
  256.     'WK4PROG5.BAS
  257.     'open a window with a button, a textbox, and a statictext
  258.     'the window is sized at 300 by 100 at position 200, 150
  259.     WindowWidth = 300
  260.     WindowHeight = 100
  261.     UpperLeftX = 200
  262.     UpperLeftY = 150
  263.     button #myFirst.ok, "OK!", [okClicked], UL, 220, 35
  264.     textbox #myFirst.field, 10, 35, 200, 25
  265.     statictext #myFirst.label, "Type some text here.", 10, 10, 150, 25
  266.     open "myname's first window!" for window as #myFirst
  267.  
  268.     'now stop and wait
  269.     input aVar$
  270.     goto [quit]
  271.  
  272. [okClicked]  'OK! was clicked.  Get the contents of the entry field
  273.  
  274.     'print a command to the textbox
  275.     print #myFirst.field, "!contents?"
  276.     'now get the contents from the textbox
  277.     input #myFirst.field, aString$
  278.  
  279.     'now pop up a notice saying what was in the textbox
  280.     notice aString$
  281.  
  282. [quit]
  283.     'now close the window
  284.     close #myFirst
  285.  
  286.     end
  287.  
  288.  
  289. See how we move the button and the textbox around in the window, and we add
  290. a text description with instructions to the user.
  291.  
  292. The format for the STATICTEXT statement is almost the same as for the TEXTBOX
  293. statement.
  294.  
  295.     statictext #myFirst.label, "Type some text here.", 10, 10, 150, 25
  296.  
  297. The first item is a string used for the text in our statictext control.
  298.  
  299. The second item #myFirst.field is a handle for the statictext.  This is just
  300. like the handle we gave to our button.
  301.  
  302. The next two items 10 and 10 are the placement of the control relative to
  303. the upper-left corner of the window.
  304.  
  305. The last two items 150 and 25 are the width and height of the control.  The
  306. size must be large enough to hold the text.  If it isn't wide enough, the end
  307. of the line will not appear.  If it isn't tall enough, you won't see any
  308. text in the control at all.
  309.  
  310.  
  311. Trapping a Window Close Event
  312. ------------------------------------------------------------------------------
  313.  
  314. When running any of our examples above we can close our custom made window
  315. at any time by double-clicking on its menu box, by pressing Alt-F4, etc.
  316. Then WINDOWS will go ahead and close the window, but Liberty BASIC will still
  317. think it's open.  This will cause some trouble if our code tries to perform
  318. some work with a window that just isn't there anymore.
  319.  
  320. The way around this problem is by sending the trapclose command to our
  321. window in question.  The trapclose command tells Liberty BASIC to hook into
  322. the WINDOWS event that closes our window.  Here is our program modified to
  323. use trapclose.
  324.  
  325.     'WK4PROG6.BAS
  326.     'open a window with a button, a textbox, and a statictext
  327.     'show how trapclose works
  328.     'the window is sized at 300 by 100 at position 200, 150
  329.     WindowWidth = 300
  330.     WindowHeight = 100
  331.     UpperLeftX = 200
  332.     UpperLeftY = 150
  333.     button #myFirst.ok, "OK!", [okClicked], UL, 220, 35
  334.     textbox #myFirst.field, 10, 35, 200, 25
  335.     statictext #myFirst.label, "Type some text here.", 10, 10, 150, 25
  336.     open "myname's first window!" for window as #myFirst
  337.  
  338.     'send the trapclose command
  339.     print #myFirst, "trapclose [quit]"
  340.  
  341. [waitHere]  'now stop and wait
  342.     input aVar$
  343.     goto [quit]
  344.  
  345. [okClicked]  'OK! was clicked.  Get the contents of the entry field
  346.  
  347.     'print a command to the textbox
  348.     print #myFirst.field, "!contents?"
  349.     'now get the contents from the textbox
  350.     input #myFirst.field, aString$
  351.  
  352.     'now pop up a notice saying what was in the textbox
  353.     notice aString$
  354.  
  355. [quit]
  356.     'ask if the user wants to quit
  357.     confirm "Really Quit?"; answer$
  358.     if answer$ <> "yes" then [waitHere] 'abort quitting
  359.  
  360.     'now close the window
  361.     close #myFirst
  362.  
  363.     end
  364.  
  365. See how the trapclose command that we print to our window also specifies the
  366. [quit] branch label.  This means that when we try to close the window using
  367. a mouse or keyboard action (using the CLOSE statement doesn't trigger
  368. trapclose), then WINDOWS will not close the window.  Instead Liberty BASIC
  369. will execute an implicit GOTO [quit].
  370.  
  371. Notice also how we added code following the [quit] branch label.  Using a
  372. CONFIRM statement we bring up a dialog box to confirm that we really do
  373. want to quit.
  374.  
  375. We also added a [waitHere] branch label in from of our INPUT statement so
  376. that we have a common place for execution to stop and wait for more user
  377. interaction.  This is pretty much standard practice in Liberty BASIC.  It
  378. really doesn't matter at which INPUT statement a Liberty BASIC stops and
  379. waits for input, but it makes things simpler if we always know where things
  380. are happening.
  381.  
  382.  
  383. Challenge Exercise
  384. ------------------------------------------------------------------------------
  385.  
  386. We can add many controls to a window in Liberty BASIC.  If we need a dozen
  387. textboxes to hold different data items and if we need a statictext to label
  388. each textbox we can do it.  Using WK4PROG6.BAS as a base, create a program
  389. that opens a window titled "Caller Record Data".  This window contains
  390. several textbox controls, each labeled by a statictext control.
  391.  
  392. The labels for our textbox controls are from our Week 3 homework:
  393.  
  394.   1) Caller's name
  395.   2) Name of person called
  396.   3) Date
  397.   4) Time of call
  398.   5) Purpose of call
  399.   6) Caller can be reached at
  400.  
  401. When the user clicks on OK!, the program should then extract all the
  402. information entered into the textbox controls and then close the window.
  403. The information should then be displayed in the program's main window.
  404.